package com.printeverywhere.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CategoryOptionsServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.SearchListCriteria;

public class ApplicationContextInterceptor implements Interceptor {

	/**	 */
	private static final long serialVersionUID = 1L;

//	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

//	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

//	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String action = ActionSupport.SUCCESS;
		action = invocation.invoke();
		ActionContext actionContext = invocation.getInvocationContext();
		SearchListCriteria searchListCriteria = new SearchListCriteria();
		searchListCriteria.setOrderBy(AdminConstants.SORTORDER);
		searchListCriteria.setAsc(AdminConstants.TRUE);
		if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_LOCATIONCATEGORIESACTION)){
			actionContext.getApplication().put(AdminConstants.LOCATIONCATEGORIES, LocationCategoriesServiceDelegator.getSortedList(searchListCriteria));
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_CATEGORYOPTIONSACTION)){
			actionContext.getApplication().put(AdminConstants.CATEGORY_OPTIONS, CategoryOptionsServiceDelegator.getCategoryOptions());
		}else {
			actionContext.getApplication().put(AdminConstants.GENERALCATEGORIES, CategoryManagerServiceDelegator.getSortedList(searchListCriteria));
		}


		return action;
	}

}
